package com.gisuni.sddc.util;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.json.JSONArray;
import org.json.JSONObject;

public class TXCallService {
	
    private	static Map<String,Object> dataMapCache=new HashMap<String,Object>();
	
    private static ReadWriteLock lock=new ReentrantReadWriteLock();//创建读写锁的实例
    
    private static Object getData(String key){
    	  lock.readLock().lock();//读取前先上锁
    	  Object val=null;
    	  try{
    	     val=dataMapCache.get(key);
    	  }finally{
    	     lock.readLock().unlock();//最后一定不要忘记释放锁
    	  }
    	  return val;
    }
    
	public static List<String> GetLoginIDAndKey() throws IOException{
		List<String> retList=new ArrayList<>();
		if (dataMapCache.containsKey("uid") && dataMapCache.get("uid")!=null && dataMapCache.containsKey("uKey") && dataMapCache.get("uKey")!=null){
			String LoginID=getData("uid").toString();
			String loginKey=getData("uKey").toString();
			retList.add(LoginID);
			retList.add(loginKey);
			
		}else{
			try {
				String strUrl=CommConfig.GPSAPIUrl+"version=1&method=loginSystem&name="+CommConfig.LoginName+"&pwd="+CommConfig.LoginPWD;
				String res=HttpUtil.get(strUrl);
				JSONObject jsonObj = new JSONObject (res);
				String LoginID= jsonObj.get("uid").toString();
				String loginKey=jsonObj.getString("uKey");
				retList.add(LoginID);
				retList.add(loginKey);
				
				dataMapCache.put("uid", LoginID);
				dataMapCache.put("uKey", loginKey);
			} catch (Exception e) {
				String LoginID= CommConfig.UID;
				String loginKey=CommConfig.UKEY;
				retList.add(LoginID);
				retList.add(loginKey);
				
				dataMapCache.put("uid", LoginID);
				dataMapCache.put("uKey", loginKey);
			}
		}
		return retList;
	}
	
	public static String GetAllVehicles() throws IOException{
		List<String> messString=TXCallService.GetLoginIDAndKey();
		String strUrl=CommConfig.GPSAPIUrl+"version=1&method=loadVehicles&uid="+messString.get(0)+"&uKey="+messString.get(1);
		
		String res=HttpUtil.get(strUrl);
		return res;
	} 
	
	public static String GetOneVehicleInfo(String vid,String vKey) throws IOException{
		String strUrl=CommConfig.GPSAPIUrl+"version=1&method=loadLocation&vid="+vid+"&vKey="+vKey;
		String res=HttpUtil.get(strUrl);
		return res;
	} 
	
	public static String GetAllLocations() throws IOException{
		List<String> messString=TXCallService.GetLoginIDAndKey();
		String strUrl=CommConfig.GPSAPIUrl+"version=1&method=loadAllLocations&uid="+messString.get(0)+"&uKey="+messString.get(1)+"&timestamp=0";
		String res=HttpUtil.get(strUrl);
		return res;
	}
	
	public static String GetHistory(String vid,String vKey,String bTime,String eTime) throws IOException, ParseException{
		SimpleDateFormat format =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date bDate = format.parse(bTime);
		Date eDate = format.parse(eTime);
		
		String strUrl=CommConfig.GPSAPIUrl+"version=1&method=loadHistory&vid="+vid+"&vKey="+vKey+"&bTime="+bDate.getTime()+"&eTime="+eDate.getTime();
		String res=HttpUtil.get(strUrl);
		return res;
	}
	
	public static Map<String, Map<String, String>> GetAllVehicleIDAndKey() {
		Map<String, Map<String, String>> returnMap=new HashMap<String, Map<String,String>>();
		
		
		try {
			String msg = GetAllVehicles();
			JSONObject jsonObj = new JSONObject (msg);
			JSONArray jsonArr=(JSONArray) jsonObj.get("groups");
			 
			for (int i=0;i<jsonArr.length();i++)
			{
				jsonObj=jsonArr.getJSONObject(i);
				JSONArray jsonVehicles=jsonObj.getJSONArray("vehicles");
				for (int j=0;j<jsonVehicles.length();j++)
				{
					JSONObject jsonVehicle=(JSONObject) jsonVehicles.get(j);
					String vehicleName=jsonVehicle.getString("name");
					String vehicleID=jsonVehicle.get("id").toString();
					String vehicleKey=jsonVehicle.getString("vKey");
					Map<String, String> child=new HashMap<>();
					child.put("ID", vehicleID);
					child.put("Key", vehicleKey);
					returnMap.put(vehicleName, child);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnMap;
	}
	
	public static String GetPeople() throws IOException{
		String strUrl=CommConfig.peopleUrl;
		Map<String,String > map = new HashMap<String,String>();
		map.put("op", "GetPersonLocation");
//		String res=HttpUtil.get(strUrl);
		String res=HttpUtil.post(strUrl,map);
		return res;
	} 
	
}
